package com.example.medicalequipment.ui.activity;

import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.medicalequipment.R;
import com.example.medicalequipment.controller.RepairController;
import com.example.medicalequipment.entity.MedicalDevice;

public class RepairDetailActivity extends AppCompatActivity {
    private MedicalDevice device;
    private RepairController repairController;
    private TextView tvDeviceName, tvDepartment, tvFaultDescription, tvReporter, tvStatus;
    private Button btnApprove, btnReject;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_repair_detail);

        // 获取设备ID
        int deviceId = getIntent().getIntExtra("deviceId", -1);
        if (deviceId == -1) {
            Toast.makeText(this, "无效的维修单ID", Toast.LENGTH_SHORT).show();
            finish();
            return;
        }

        // 初始化视图和控制器
        initViews();
        repairController = new RepairController(this, null);

        // 加载维修单详情
        loadRepairDetail(deviceId);
    }

    private void initViews() {
        tvDeviceName = findViewById(R.id.tv_device_name);
        tvDepartment = findViewById(R.id.tv_department);
        tvFaultDescription = findViewById(R.id.tv_fault_description);
        tvReporter = findViewById(R.id.tv_reporter);
        tvStatus = findViewById(R.id.tv_status);
        btnApprove = findViewById(R.id.btn_approve);
        btnReject = findViewById(R.id.btn_reject);

        // 设置按钮点击事件
        btnApprove.setOnClickListener(v -> approveRepair());
        btnReject.setOnClickListener(v -> rejectRepair());
    }

    private void loadRepairDetail(int deviceId) {
        // 在后台线程中获取维修单详情
        new AsyncTask<Integer, Void, MedicalDevice>() {
            @Override
            protected MedicalDevice doInBackground(Integer... ids) {
                return repairController.getRepairRequestById(ids[0]);
            }

            @Override
            protected void onPostExecute(MedicalDevice result) {
                if (result != null) {
                    device = result;
                    updateUI();
                } else {
                    Toast.makeText(RepairDetailActivity.this, "维修单不存在", Toast.LENGTH_SHORT).show();
                    finish();
                }
            }
        }.execute(deviceId);
    }

    private void updateUI() {
        tvDeviceName.setText(device.getName());
        tvDepartment.setText(device.getDepartment());
        tvFaultDescription.setText(device.getFaultDescription());
        tvReporter.setText(device.getReporter());

        // 根据状态显示不同文本
        String statusText = "";
        switch (device.getStatus()) {
            case 0:
                statusText = "待处理";
                btnApprove.setVisibility(View.VISIBLE);
                btnReject.setVisibility(View.VISIBLE);
                break;
            case 1:
                statusText = "处理中";
                break;
            case 2:
                statusText = "已完成";
                break;
        }
        tvStatus.setText("状态: " + statusText);
    }

    private void approveRepair() {
        if (device != null) {
            device.setStatus(1); // 设置为处理中
            repairController.updateRepairStatus(device, new RepairController.OnUpdateListener() {
                @Override
                public void onUpdate(boolean success) {
                    if (success) {
                        Toast.makeText(RepairDetailActivity.this, "已批准维修申请", Toast.LENGTH_SHORT).show();
                        setResult(RESULT_OK);
                        finish();
                    } else {
                        Toast.makeText(RepairDetailActivity.this, "批准失败，请重试", Toast.LENGTH_SHORT).show();
                    }
                }
            });
        }
    }

    private void rejectRepair() {
        if (device != null) {
            device.setStatus(2); // 设置为已拒绝
            repairController.updateRepairStatus(device, new RepairController.OnUpdateListener() {
                @Override
                public void onUpdate(boolean success) {
                    if (success) {
                        Toast.makeText(RepairDetailActivity.this, "已拒绝维修申请", Toast.LENGTH_SHORT).show();
                        setResult(RESULT_OK);
                        finish();
                    } else {
                        Toast.makeText(RepairDetailActivity.this, "操作失败，请重试", Toast.LENGTH_SHORT).show();
                    }
                }
            });
        }
    }
}